package com.beck.util;
import com.lowagie.text.*;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;
public class PdfHeaderFooter extends PdfPageEventHelper {
    //Font bfChinese;
    int fontSize = 10;
    Image image;

    @Override
    public void onOpenDocument(PdfWriter writer, Document document) {
        try {
           // bfChinese = new Font(BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), fontSize, Font.NORMAL);
            image = Image.getInstance("D:\\test\\java-pdf-master\\java-pdf-master\\study_simple_demo-master\\src\\main\\resources\\template\\logo.png");
            image.scalePercent(50);
            image.setAbsolutePosition(8, document.top());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        try {
            Rectangle rect = new Rectangle(0, 38, 50, 50);

            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_BOTTOM,
                    new Phrase(String.format("- %d -", writer.getPageNumber())), (rect.getLeft() + rect.getRight()) / 2,
                    rect.getBottom() - 18, 0);

            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_TOP,
                    new Phrase("我是页眉"), (document.left() + document.right()) / 2,
                    document.top() + 18, 0);


        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}